#include "comm_driver.h"
#include "Arduino.h"
#include <iostream>                      
#include <stdlib.h>



CommDriver::CommDriver(int comPort)
{

    m_comPort=comPort;
    if(m_comPort==0){
         Serial.begin(115200);
    }else if(m_comPort==1){
        Serial1.begin(115200, SERIAL_8N1, 23, 22); 
    }else if(m_comPort==2){
         //D17:Rx  D16:Tx
         Serial2.begin(115200, SERIAL_8N1, 17, 16); 
    }
}


CommDriver::~CommDriver()
{
  
}



uint32_t CommDriver::Send(uint8_t* srcBuf, uint32_t toSendLen)
{
    if(m_comPort==0){
        Serial.write(srcBuf,toSendLen);
        Serial.flush();
    }else if(m_comPort==1){
        Serial1.write(srcBuf,toSendLen);
        Serial1.flush();
    }else if(m_comPort==2){
        Serial2.write(srcBuf,toSendLen);
        Serial2.flush();
    }
    return toSendLen;
}


uint32_t CommDriver::Receive(uint8_t* dstBuf, uint32_t toRecvSize)
{  
   int numBytes=0;
  // dstBuf[numBytes++]=m_comPort;
   if(m_comPort==0){
        while(Serial.available()){
             char c = Serial.read();
             dstBuf[numBytes++]=c;
        }
   }else if(m_comPort==1){
        while(Serial1.available()){
             char c = Serial1.read();
             dstBuf[numBytes++]=c;
        }
   }else if(m_comPort==2){
        while(Serial2.available()){
             char c = Serial2.read();
             dstBuf[numBytes++]=c;
        }
   }
   return numBytes>0?numBytes:0;
}
